package com.study.talk.utils.alert

import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.view.View
import android.widget.TextView
import com.study.talk.R

object AlertUtil {
    /**
     * @description: 显示弹窗
     * @param: activity 用于获取上下文的activity
     * @param: title 对话框标题
     * @param: msg 对话框内容
     * @param: then 对话框选项点击的回调
     * @return: void
     * @author thor
     * @date: 2024/7/8 9:58
     */
    fun showCustomDialog(
        context: Context, title: String, msg: String, callback: (Boolean) -> Unit
    ): CustomDialog {
        val dialog = CustomDialog(context)
        dialog.createView(context, R.layout.alert).apply {
            findViewById<TextView>(R.id.alert_title).text = title
            findViewById<TextView>(R.id.alert_message).text = msg
            findViewById<View>(R.id.alert_ok).setOnClickListener {
                callback.invoke(true)
                dialog.dismiss()
            }
            findViewById<View>(R.id.alert_cancel).setOnClickListener {
                callback.invoke(false)
                dialog.dismiss()
            }
        }
        dialog.setCanceledOnTouchOutside(false)
        dialog.show()
        return dialog
    }

    /**
     * @description: 显示弹窗 无取消按钮 无点击确认后的回调事件
     * @param: activity 用于获取上下文的activity
     * @param: title 对话框标题
     * @param: msg 对话框内容
     * @return: void
     * @author thor
     * @date: 2024/7/8 9:58
     */
    fun show(context: Context, title: String, msg: String): AlertDialog {
        val builder = AlertDialog.Builder(context)
        builder.setTitle(title).setMessage(msg)
            .setPositiveButton("确定") { dialog: DialogInterface, _ ->
                dialog.dismiss()
            }.setCancelable(false)
        return builder.show()
    }

    /**
     * @description: 显示弹窗
     * @param: activity 用于获取上下文的activity
     * @param: title 对话框标题
     * @param: msg 对话框内容
     * @param: then 回调函数
     * @return: void
     * @author thor
     * @date: 2024/7/8 9:58
     */
    fun showEnsureAction(
        context: Context, title: String, msg: String, callback: (Boolean) -> Unit
    ) {
        val builder = AlertDialog.Builder(context)
        builder.setTitle(title).setMessage(msg)
            .setPositiveButton("确定") { dialogInterface: DialogInterface, _ ->
                dialogInterface.dismiss()
                callback.invoke(true)
            }.setCancelable(false)
        builder.show()
    }

    /**
     * @description: 显示弹窗
     * @param: activity 用于获取上下文的activity
     * @param: title 对话框标题
     * @param: msg 对话框内容
     * @param: then 回调函数
     * @return: void
     * @author thor
     * @date: 2024/7/8 9:58
     */
    fun showAllAction(
        context: Context, title: String?, msg: String?, callback: (Boolean) -> Unit
    ) {
        val builder = AlertDialog.Builder(context)
        builder.setTitle(title).setMessage(msg)
            .setPositiveButton("确定") { dialog: DialogInterface, _ ->
                dialog.dismiss()
                callback.invoke(true)
            }.setNegativeButton(
                "取消"
            ) { dialog: DialogInterface, _ ->
                dialog.dismiss()
                callback.invoke(false)
            }.setCancelable(false)
        builder.show()
    }
}
